<template>
    <div>
        <el-button type="primary" @click="dialogFormVisible = true">添 加</el-button>
        <el-dialog title="品牌信息" :visible.sync="dialogFormVisible">
            <el-form :model="form">
                <el-form-item label="品牌名称">
                    <el-input v-model="form.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="首字母">
                    <el-input v-model="form.letter" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="图片">
                    <el-upload
                            class="avatar-uploader"
                            action="http://api.blb.com/api/upload-api/upload"
                            :show-file-list="false"
                            :on-success="handleUploadSuccess">
                        <img v-if="form.image" :src="form.image" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveBrand">确 定</el-button>
            </div>
        </el-dialog>
        <el-table
                :data="tableData"
                style="width: 100%">
            <el-table-column
                    label="编号"
                    width="180">
                <template slot-scope="scope">
                    <i class="el-icon-time"></i>
                    <span style="margin-left: 10px">{{ scope.row.id }}</span>
                </template>
            </el-table-column>
            <el-table-column
                    label="名称"
                    width="180">
                <template slot-scope="scope">
                    <i class="el-icon-time"></i>
                    <span style="margin-left: 10px">{{ scope.row.name }}</span>
                </template>
            </el-table-column>
            <el-table-column
                    label="首字母"
                    width="180">
                <template slot-scope="scope">
                    <i class="el-icon-time"></i>
                    <span style="margin-left: 10px">{{ scope.row.letter }}</span>
                </template>
            </el-table-column>
            <el-table-column
                    label="图片"
                    width="180">
                <template slot-scope="scope">
                    <el-image :src="scope.row.image"></el-image>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                background
                layout="prev, pager, next"
                :total="total"
                :current-page="current"
                @current-change="loadPage">
        </el-pagination>
    </div>
</template>

<script>
    export default {
        name: "Brand",
        data() {
            return {
                tableData:[],
                total:10,
                current:1,
                dialogFormVisible:false,
                form:{id:0,name:"",image:"",letter:""}
            };
        },
        methods: {
            loadPage(page){
                this.$http.get("http://api.blb.com/api/goods-api/brand/page/"+page)
                    .then(res => {
                       if(res.data.code == 1){
                           this.tableData = res.data.data.records;
                           this.total = res.data.data.total;
                           this.current = res.data.data.current;
                       }
                    });
            },
            handleUploadSuccess(res,file){
                this.form.image = res.url;
            },
            saveBrand(){
                this.$http.post("http://api.blb.com/api/goods-api/brand",this.form)
                    .then(res => {
                       if(res.data.code == 1){
                           this.$message("保存成功");
                       }
                    });
                this.dialogFormVisible = false;
            }
        },
        mounted() {
            this.loadPage(1);
        }
    }
</script>

<style scoped>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>